<template>
  <section class="page-container">
    <div class="page-search">
      <el-form :model="search" inline label-suffix=":" ref="search">
        <el-form-item prop="username" label="用户名">
          <el-input
            style="width: 200px"
            prefix-icon="el-icon-search"
            v-model="search.username"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryTable">查询</el-button>
          <el-button @click="resetForm('search')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="page-table-tool">
      <el-button type="primary" @click="visible = true">新增账号</el-button>
    </div>
    <div class="page-table">
      <el-table :data="dataList" height="100%" border :stripe="true">
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="创建时间" prop="timeCreated">
          <template slot-scope="scope">
            {{ scope.row.timeCreated | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          prop="timeCreated"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" class="danger">
              <el-popconfirm
                title="是否确认删除？"
                @confirm="handleDelete(scope.row)"
              >
                <span slot="reference">删除</span>
              </el-popconfirm>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-bottom">
      <el-pagination
        class="pagination"
        v-bind="pageProp"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog
      width="500px"
      :title="form.id ? '修改用户信息' : '创建用户'"
      :visible.sync="visible"
    >
      <el-form
        :model="form"
        label-suffix=":"
        label-width="80px"
        ref="form"
        :rules="rules"
      >
        <el-form-item prop="username" label="用户名">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="handleSubmit"> 保 存 </el-button>
      </span>
    </el-dialog>
  </section>
</template>

<script>
import tableProps from "@/mixins/tableProps";
export default {
  name: "admin",
  mixins: [tableProps],
  data() {
    return {
      dataList: [],
      search: {},
      form: {},
      visible: false,
    };
  },
  created() {
    this.queryTable();
  },
  computed: {
    rules() {
      return {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      };
    },
  },
  watch: {
    visible(val) {
      if (!val) {
        this.form = {};
      }
    },
  },
  methods: {
    queryTable() {
      const data = {
        ...this.getParams,
        ...this.search,
      };

      this.$api.post("/system/admin/all", data).then((response) => {
        this.dataList = response.dataSet;
        this.updatePage("total", response.total);
      });
    },
    handleEdit(row) {
      const form = {
        id: row.id,
        username: row.username,
      };
      this.form = form;
      this.visible = true;
    },
    handleDelete(row) {
      this.$api
        .post("/system/admin/delete", { id: row.id }, { showMessage: true })
        .then(() => {
          this.queryTable();
        });
    },
    handleSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let path = "/system/admin/add";
          if (this.form.id) {
            path = "/system/admin/update";
          }

          this.$api.post(path, this.form, { showMessage: true }).then(() => {
            this.visible = false;
            this.queryTable();
          });
        }
      });
    },
  },
};
</script>
